﻿Public Class ClsIndustryAccountGroupDbAccess
    Inherits ClsBaseDbAccess
    '对应表：S_IndustryAcctGroup

    Public Sub New(ByVal s As String)
        MyBase.New(s, TABLE_NAME_INDUSTRY_ACCOUNT_GROUP)
    End Sub

    Public Sub New(ByVal s As String, ByVal n As String)
        MyBase.New(s, n)
    End Sub

    Public Function GetGroupsByTypeId(ByVal gId As Integer) As List(Of ClsIndustryAccountGroup)
        Try

            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection()
            Dim tmpParas(0) As OleDb.OleDbParameter
            Dim tmpRet As List(Of ClsIndustryAccountGroup)
            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "Select * from [" & Me.AppTableName & "] "
            tmpSql &= " Where [IndustryId] = @IndustryId Order by [AcctGroupNumber] asc "

            '构造参数集合
            tmpParas(0) = New OleDb.OleDbParameter("@IndustryId", OleDb.OleDbType.Integer)
            tmpParas(0).Value = gId

            '查询并将返回数据解析为List对象
            tmpRet = CType(ConvertRecordToClass(tmpDbAccess.ExecuteSqlReturnDataSet(tmpSql, tmpParas)), List(Of ClsIndustryAccountGroup))

            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpRet

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsIndustryAccountGroupDbAccess - GetGroupsByIndustryId"

            Return Nothing
        End Try
    End Function

    Public Overrides Function ConvertRecordToClass(reader As OleDb.OleDbDataReader) As Object
        If reader Is Nothing Then
            Return Nothing
        End If

        Dim tmpClsList As List(Of ClsIndustryAccountGroup) = New List(Of ClsIndustryAccountGroup)

        While reader.Read()

            Dim tmpCls As ClsIndustryAccountGroup = New ClsIndustryAccountGroup()

            With tmpCls
                .IndustryAcctGroupId = reader.GetInt32(0)
                .IndustryId = reader.GetInt32(1)
                .AcctGroupNumber = reader.GetInt16(2) '数据库中的整数是 16，长整数是32，二者并不能互换
                .AcctGroupName = reader.GetString(3)

            End With

            tmpClsList.Add(tmpCls)

        End While


        Return tmpClsList
    End Function

End Class
